package jfdi;

import util.Mlog;

import java.util.List;

public class VerticalLayout extends Layout {

    public VerticalLayout(NodeGroup host) {
        super(host);
    }

    public int getMinWidth() {
        List<Node> nodes = mNode.getNodes();
        final int n = nodes.size();
        int max = 0;
        for (int i = 0; i < n; i++) {
            final Node node = nodes.get(i);
            int mh = node.getMinWidth();
            if (mh == 0) {
                return 0;
            } else {
                if (mh > max) max = mh;
            }
        }
        return max;
    }

    public int getMinHeight() {
        List<Node> nodes = mNode.getNodes();
        final int n = nodes.size();
        int total = 0;
        for (int i = 0; i < n; i++) {
            final Node node = nodes.get(i);
            int mh = node.getMinHeight();
            if (mh == 0) {
                return 0;
            } else {
                total += mh;
            }
        }
        return total;
    }

    public void layout(int l, int t, int r, int b) {
        List<Node> nodes = mNode.getNodes();
        final int n = nodes.size();
        int count = 0;
        int height = b - t;
        for (int i = 0; i < n; i++) {
            final Node node = nodes.get(i);
            int mh = node.getMinHeight();
            if (mh == 0) {
                count++;
            } else {
                height -= mh;
            }
        }
        float nodeh = (count > 0) ? height / (float) count : 0;
        int top = t;
        for (int i = 0; i < n; i++) {
            final Node node = nodes.get(i);
            int mh = node.getMinHeight();
            if (mh == 0) {
                int bot = (int) (top + nodeh + 0.5f);
                node.layout(l, top, r, bot);
                top = bot;
            } else {
                node.layout(l, top, r, top + mh);
                top += mh;
            }

        }
        mIsLayedOut = true;
    }

}
